<template>
  <basic-container>
    <!--  :before-open="beforeOpen" -->
    <avue-crud
      ref="crud"
      :data="data"
      :option="option"
      :page.sync="page"
      :search.sync="query"
      @search-change="searchChange"
      @on-load="rowLoad"
      @row-update="rowUpdate"
      @row-save="rowSave"
    ></avue-crud>
  </basic-container>
</template>

<script>
import avue from "@/mixins/avue.js";
import { SE } from "@/common/constants.js";
import {
  channel_list,
  channelMenu_tree,
  channelMenu_add,
  channelMenu_edit,
} from "@/api/core/system";

export default {
  mixins: [avue],
  data() {
    return {
      data: [],
      option: {
        dialogDirection: "rtl",
        dialogType: "drawer",
        column: [
          {
            label: "应用渠道",
            prop: "channelCode",
            search: true,
            hide: true,
            type: "select",
            dicData: [],
            props: {
              label: "name",
              value: "code",
            },
          },
          {
            label: "菜单名称",
            prop: "name",
            width: 120,
          },
          {
            label: "类型",
            prop: "type",
            type: "select",
            dicData: [
              {
                label: "菜单",
                value: "0",
              },
              {
                label: "外链",
                value: "links",
              },
            ],
            value: "0",
            width: 100,
          },
          {
            label: "上级菜单",
            prop: "parentId",
            type: "tree",
            props: {
              label: "name",
              value: "id",
            },
            dicdata: [],
            // propsHttp: {
            //   home: baseUrl2,
            //   res: "data",
            //   url: "path",
            // },
            // dicFormatter: (res) => {
            //   return res.data; //返回字典的层级结构
            // },
            hide: true,
          },
          {
            label: "访问路径",
            prop: "url",
            align: "left",
            // hide: true
          },
          // {
          //   label: '组件目录',
          //   prop: 'component',
          //   align: 'left',
          //   hide: true
          // },
          {
            label: "图标选择",
            prop: "icon",
          },
          {
            label: "状态",
            prop: "status",
            type: "radio",
            dicData: SE,
            value: "1",
            width: 100,
          },
          {
            label: "排序",
            prop: "sort",
            type: "number",
            value: 0,
            width: 100,
          },
          {
            label: "拓展信息",
            prop: "extra",
            type: "textarea",
            hide: true,
          },
        ],
      },
    };
  },
  created() {
    channel_list().then((res) => {
      const result = this.$s.res(res);
      this.findObject(this.option.column, "channelCode").dicData = result;
      this.findObject(this.option.column, "channelCode").searchValue =
        result[0].code;
      this.query.channelCode = result[0].code;
      this.params.channelCode = result[0].code;
    });
  },
  methods: {
    beforeOpen(done, type) {
      // if (['edit', 'add'].includes(type)) {
      //   var crud = this.$refs.crud
      //   channelMenu_tree({ type: 1 }).then((res) => {
      //     crud.updateDic('parentId', this.$s.res(res))
      //   })
      // }
      done();
    },
    rowLoad() {
      channelMenu_tree(this.params).then((res) => {
        this.data = this.$s.res(res);
      });
    },
    rowSave(row, done, loading) {
      channelMenu_add(row)
        .then((res) => {
          this.$s.suc(res);
          loading();
          this.rowLoad();
          done();
        })
        .catch((err) => {
          console.error(err);
          loading();
        });
    },
    rowUpdate(row, index, done, loading) {
      channelMenu_edit(row)
        .then((res) => {
          this.$s.suc(res);
          loading();
          this.rowLoad();
          done();
        })
        .catch((err) => {
          console.error(err);
          loading();
        });
    },
    rowDel(row) {
      this.$confirm("确定数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return this.request({
            url: "2/api/BusCtgReservePlan/remove",
            method: "post",
            params: {
              id: row.id,
            },
          });
        })
        .then(() => {
          this.rowLoad();
        });
    },
  },
  mounted() {},
};
</script>

<style></style>
